<?php
class IndexAction extends Action{
    public function index(){
    
    //检测是否已经安装
      $file = "./public/Install/install.lock";
      if(file_exists($file)){
        $this->redirect('Home/Index/index');
      }
      
      //检测是否同意安装
      if($_POST['install']){
      $this->redirect('Install/Index/check');
      
      }else{
      $this->assign('l', cookie('think_language'));
       $this->display();
      }
    }
    
        public function check(){
        
         $file = "./public/Install/install.lock";
      if(file_exists($file)){
        $this->redirect('Home/Index/index');
      }
      $arr  = array();
      $c_f = array(
      './Uploads','./Public','./App/Conf','./App/Runtime',
      );
      foreach($c_f as $file){
      if(is_dir($file)){
      $ftest = @fopen($file.'/t.txt','w');
      if($ftest){
        $arr[$file] =true;   
      }else{
        $arr[$file] = false;
      }
      @fclose($ftest);
      @unlink($file.'/t.txt');
      }
      }
      
      $arr1  = array(
        'GD Library' => isfun('gd_info'),
        'curl' => isfun('curl_close'),
        'Memcache' => isfun('Memcache_close'),
        'mysqli' => isfun('mysqli_close'),
        'sqlite' => isfun('sqlite_close'),
        'PREL' => isfun('preg_match'),
        'SQL Server' => isfun('mssql_close'),
        'session' => isfun('session_start'),
      );
      
      $arrs = array_merge($arr,$arr1);
			$this->assign('emsg', $arrs);
		  $this->display();
			

    }
    
    public function config(){
       	$file = './Public/Install/install.lock';
		if (file_exists($file)){
			$this->redirect('Home/Index/index');
		}
		
		$arrc = array(
      'fuwuqi'=>'localhost',
      'duankou'=>'3306',
      'dbuser'=>'root',
      'dbpwd'=>'',
      'dbname'=>'aicms',
      'dbzhui'=>'i_',
		);
		$this->assign('arrc',$arrc);
		if(isset($_POST['cfg'])){
		foreach ($_POST as $key=>$val) {
				$this->assign($key, $val);
			}
      extract($_POST);
      $mulu = trim($mulu);
       $fuwuqi = trim($fuwuqi);
       $duankou = trim($duankou);
      $dbuser =  trim($dbuser);
      $dbpwd = trim($dbpwd);
      $dbname = trim($dbname);
       $dbzhui = trim($dbzhui);
      $user  = trim($user);
       $pwd = trim($pwd);
       $pwd2 = trim($pwd2);
      $e = trim($e);
 
      //是否为空
      if(!$fuwuqi || !$duankou || !$dbuser || !$dbname || !$dbzhui || !$user || !$pwd || !$pwd2 || !$e ){
      $this->assign('errormsg',L('i30'));
      $this->display();
      exit;
      }
      
      //密码长度
      if(strlen($pwd)<5 || strlen($pwd)>20){
				$this->assign('errormsg',L('i31'));
				$this->display();
				exit;
			}
			
			//验证密码是否一致
					if ($pwd != $pwd2) {
				$this->assign('errormsg',L('i32'));
				$this->display();
				exit;
			}
			
				//检查email格式
			if (!is_email($e)) {
				$this->assign('errormsg',L('i33'));
				$this->display();
				exit;
			}
			
				//检测数据库连接
			$conn = @mysql_connect($fuwuqi,$dbuser,$dbpwd);
			if (!$conn) {
				$this->assign('errormsg',L('i34'));
				$this->display();
				exit;
			}
			
			//检测数据库是否存在，不存在则创建数据库
			if (!@mysql_select_db($db_name)){
				$sql = "CREATE DATABASE `".$dbname."` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
				mysql_query($sql);
			}
			
			//检测数据库是否创建成功
			if(!@mysql_select_db($dbname)){
				$this->assign('errormsg',L('i35'));
				$this->display();
				exit;
			}
			
			//保存配置
			$cg = array(
        'web_path' =>$mulu,
        'db_host' =>$fuwuqi,
        'db_port' =>$duankou,
        'db_user' =>$dbuser,
        'db_pwd' =>$dbpwd,
        'db_name' =>$dbname,
        'db_prefix' =>$dbzhui,
			);
			updateconfig($cg);
			
			//导入sql
			$sqls = get_sql('./Public/sql/install.sql');
			$conn = @mysql_connect($fuwuqi,$dbuser,$dbpwd);
			mysql_select_db($dbname);
			
			foreach ($sqls as $sql){
				//替换表前缀
				$sql = str_replace('ai_',$dbzhui,$sql);
				$result = mysql_query($sql,$conn);
				if (@mysql_affected_rows($conn)<0){
					$this->assign('errormsg',L('i36'));
					$this->display();
					exit;
				}
			}
			
			//添加网站域名
				$domain = $cg[cms_domain] = $cg[site_domain] = 'http://'.$_SERVER['HTTP_HOST'].$mulu;
			updateconfig($cg);
			
			//添加管理员
			$pwd = md5($pwd);
			$addtime = time();
			$query = "INSERT INTO ".$dbzhui."user(id,username,password,creattime,email) VALUES(null,'".$user."','".$pwd."','.$addtime.','".$e."' )";
			$result = mysql_query($query);	
			if (@mysql_affected_rows($conn)<0){
				$this->assign('errormsg',L('i37'));
				$this->display();
				exit;
			}else{
			fopen('./Public/Install/install.lock','w');
				$this->redirect('Install/Index/finish');
			}
      
		}
     $this->display();
    }

    public function finish(){
       $this->display();
    
    }
    
    public function lange(){
      $lange = $this->_request('l');
      cookie('think_language',$lange);
      $this->success(L('Set Language Success'), 'index');

    }
   
}